package com.voicecomm.database;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * 通联日志实体类
 * 记录语音通联的详细信息
 * 
 * @author VoiceComm Team
 * @version 1.0.0
 */
public class CommunicationLog {
    
    @JsonProperty("id")
    private Long id;
    
    @JsonProperty("session_id")
    private String sessionId;
    
    @JsonProperty("caller_id")
    private String callerId;
    
    @JsonProperty("receiver_id")
    private String receiverId;
    
    @JsonProperty("start_time")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime startTime;
    
    @JsonProperty("end_time")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime endTime;
    
    @JsonProperty("duration_seconds")
    private Integer durationSeconds;
    
    @JsonProperty("recognition_text")
    private String recognitionText;
    
    @JsonProperty("confidence_score")
    private Double confidenceScore;
    
    @JsonProperty("audio_file_path")
    private String audioFilePath;
    
    @JsonProperty("status")
    private String status; // ACTIVE, COMPLETED, FAILED
    
    @JsonProperty("notes")
    private String notes;

    // ==== 新增结构化抽取字段 ====
    @JsonProperty("qth")
    private String qth; // 地址/QTH

    @JsonProperty("device_model")
    private String deviceModel; // 设备型号/电台型号

    @JsonProperty("signal_report")
    private String signalReport; // 信号报告 RST，如 59/579/599

    @JsonProperty("antenna_type")
    private String antennaType; // 天线类型/名称

    @JsonProperty("antenna_height_m")
    private Double antennaHeightMeters; // 天线高度(米)
    
    @JsonProperty("created_at")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createdAt;
    
    @JsonProperty("updated_at")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updatedAt;
    
    // 构造函数
    public CommunicationLog() {
        this.createdAt = LocalDateTime.now();
        this.updatedAt = LocalDateTime.now();
    }
    
    public CommunicationLog(String sessionId, String callerId, String receiverId) {
        this();
        this.sessionId = sessionId;
        this.callerId = callerId;
        this.receiverId = receiverId;
        this.startTime = LocalDateTime.now();
        this.status = "ACTIVE";
    }
    
    // Getter和Setter方法
    public Long getId() {
        return id;
    }
    
    public void setId(Long id) {
        this.id = id;
    }
    
    public String getSessionId() {
        return sessionId;
    }
    
    public void setSessionId(String sessionId) {
        this.sessionId = sessionId;
    }
    
    public String getCallerId() {
        return callerId;
    }
    
    public void setCallerId(String callerId) {
        this.callerId = callerId;
    }
    
    public String getReceiverId() {
        return receiverId;
    }
    
    public void setReceiverId(String receiverId) {
        this.receiverId = receiverId;
    }
    
    public LocalDateTime getStartTime() {
        return startTime;
    }
    
    public void setStartTime(LocalDateTime startTime) {
        this.startTime = startTime;
    }
    
    public LocalDateTime getEndTime() {
        return endTime;
    }
    
    public void setEndTime(LocalDateTime endTime) {
        this.endTime = endTime;
        if (startTime != null && endTime != null) {
            this.durationSeconds = (int) java.time.Duration.between(startTime, endTime).getSeconds();
        }
    }
    
    public Integer getDurationSeconds() {
        return durationSeconds;
    }
    
    public void setDurationSeconds(Integer durationSeconds) {
        this.durationSeconds = durationSeconds;
    }
    
    public String getRecognitionText() {
        return recognitionText;
    }
    
    public void setRecognitionText(String recognitionText) {
        this.recognitionText = recognitionText;
        this.updatedAt = LocalDateTime.now();
    }
    
    public Double getConfidenceScore() {
        return confidenceScore;
    }
    
    public void setConfidenceScore(Double confidenceScore) {
        this.confidenceScore = confidenceScore;
        this.updatedAt = LocalDateTime.now();
    }
    
    public String getAudioFilePath() {
        return audioFilePath;
    }
    
    public void setAudioFilePath(String audioFilePath) {
        this.audioFilePath = audioFilePath;
        this.updatedAt = LocalDateTime.now();
    }
    
    public String getStatus() {
        return status;
    }
    
    public void setStatus(String status) {
        this.status = status;
        this.updatedAt = LocalDateTime.now();
    }
    
    public String getNotes() {
        return notes;
    }
    
    public void setNotes(String notes) {
        this.notes = notes;
        this.updatedAt = LocalDateTime.now();
    }

    public String getQth() {
        return qth;
    }

    public void setQth(String qth) {
        this.qth = qth;
        this.updatedAt = LocalDateTime.now();
    }

    public String getDeviceModel() {
        return deviceModel;
    }

    public void setDeviceModel(String deviceModel) {
        this.deviceModel = deviceModel;
        this.updatedAt = LocalDateTime.now();
    }

    public String getSignalReport() {
        return signalReport;
    }

    public void setSignalReport(String signalReport) {
        this.signalReport = signalReport;
        this.updatedAt = LocalDateTime.now();
    }

    public String getAntennaType() {
        return antennaType;
    }

    public void setAntennaType(String antennaType) {
        this.antennaType = antennaType;
        this.updatedAt = LocalDateTime.now();
    }

    public Double getAntennaHeightMeters() {
        return antennaHeightMeters;
    }

    public void setAntennaHeightMeters(Double antennaHeightMeters) {
        this.antennaHeightMeters = antennaHeightMeters;
        this.updatedAt = LocalDateTime.now();
    }
    
    public LocalDateTime getCreatedAt() {
        return createdAt;
    }
    
    public void setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }
    
    public LocalDateTime getUpdatedAt() {
        return updatedAt;
    }
    
    public void setUpdatedAt(LocalDateTime updatedAt) {
        this.updatedAt = updatedAt;
    }
    
    /**
     * 结束通联会话
     */
    public void endSession() {
        this.endTime = LocalDateTime.now();
        this.status = "COMPLETED";
        this.updatedAt = LocalDateTime.now();
        
        if (startTime != null) {
            this.durationSeconds = (int) java.time.Duration.between(startTime, endTime).getSeconds();
        }
    }
    
    /**
     * 添加识别文本（追加模式）
     * 
     * @param newText 新的识别文本
     */
    public void appendRecognitionText(String newText) {
        if (newText != null && !newText.trim().isEmpty()) {
            if (this.recognitionText == null) {
                this.recognitionText = newText;
            } else {
                this.recognitionText += " " + newText;
            }
            this.updatedAt = LocalDateTime.now();
        }
    }
    
    /**
     * 检查会话是否活跃
     * 
     * @return true如果会话活跃，false否则
     */
    public boolean isActive() {
        return "ACTIVE".equals(this.status);
    }
    
    /**
     * 检查会话是否已完成
     * 
     * @return true如果会话已完成，false否则
     */
    public boolean isCompleted() {
        return "COMPLETED".equals(this.status);
    }
    
    /**
     * 获取会话持续时间（秒）
     * 
     * @return 持续时间，如果会话未结束则返回当前持续时间
     */
    public int getCurrentDuration() {
        if (startTime == null) {
            return 0;
        }
        
        LocalDateTime endTime = this.endTime != null ? this.endTime : LocalDateTime.now();
        return (int) java.time.Duration.between(startTime, endTime).getSeconds();
    }
    
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        CommunicationLog that = (CommunicationLog) o;
        return Objects.equals(id, that.id) &&
               Objects.equals(sessionId, that.sessionId);
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(id, sessionId);
    }
    
    @Override
    public String toString() {
        return String.format("CommunicationLog{id=%d, sessionId='%s', callerId='%s', receiverId='%s', " +
                           "startTime=%s, endTime=%s, duration=%ds, status='%s', text='%s'}", 
                           id, sessionId, callerId, receiverId, startTime, endTime, 
                           durationSeconds, status, 
                           recognitionText != null ? recognitionText.substring(0, Math.min(50, recognitionText.length())) + "..." : "null");
    }
}
